package com.husd.framework.ddl_new;

import org.apache.commons.lang3.StringUtils;

import java.util.List;
import java.util.Stack;

/**
 * @author shengdonghu@126.com
 * @date 2024/1/22 17:42
 */
public class WordListScanner {

    private List<String> wordList;

    private int index = 0; // 当前索引
    private int len = 0;

    public WordListScanner(List<String> wordList) {
        this.wordList = wordList;
        this.len = this.wordList.size();
    }

    public String current() {

        if (index >= len) {
            throw new RuntimeException("无效的index:" + index);
        }
        return this.wordList.get(index);
    }

    public String getWordByIndex(int index) {

        if (!validIndex(index)) {
            throw new RuntimeException("调用 currentWord() 方法错误 当前的index:" + index + " 无效");
        }
        String word = wordList.get(index);
        if (word != null) {
            return word.toLowerCase();
        }
        return word;
    }

    private boolean validIndex(int index) {

        return index >= 0 && index <= (len - 1);
    }

    public boolean haveWord() {

        return index <= this.len - 1;
    }

    public boolean haveNext() {

        return index <= this.len - 2;
    }

    public boolean next() {
        boolean c = this.haveNext();
        this.index++;
        return c;
    }

    public String getNextWord() {

        if (this.haveNext()) {
            return getWordByIndex(this.index + 1);
        }
        return null;
    }

    public String getNextNextWord() {

        if (validIndex(this.index + 2)) {
            return getWordByIndex(this.index + 2);
        }
        return null;
    }

    public boolean findWord(String ti) {

        int i = index;
        while (this.validIndex(i)) {
            String w = this.getWordByIndex(i);
            if (StringUtils.equalsIgnoreCase(w, ti)) {
                return true;
            }
            i++;
        }
        return false;
    }

    public String getPreWord() {

        int i = this.index - 1;
        if (this.validIndex(i)) {
            return this.getWordByIndex(i);
        }
        return null;
    }

    public String getPrePreWord() {

        int i = this.index - 2;
        if (this.validIndex(i)) {
            return this.getWordByIndex(i);
        }
        return null;
    }

    public String debug() {

        return this.getPreWord() + " " + this.current() + " " + this.getNextWord() + " " + this.getNextNextWord();
    }

    public String findMatchSamePair(String leftPair) {

        String _c = current();
        if (!StringUtils.equalsIgnoreCase(leftPair, _c)) {
            throw new RuntimeException("findMatchPair 方法异常 当前字符开始不是leftPair leftPair:" + leftPair + " debug:" + debug());
        }
        StringBuilder sb = new StringBuilder();
        this.next();
        _c = current();
        while (!StringUtils.equalsIgnoreCase(_c, leftPair)) {
            sb.append(_c).append(" ");
            if (this.haveNext()) {
                this.next();
                _c = current();
            } else {
                break;
            }
        }
        return sb.toString();
    }

    public String findMatchDiffPair(String leftPair, String rightPair) {

        String _c = current();
        if (!StringUtils.equalsIgnoreCase(leftPair, _c)) {
            throw new RuntimeException("findMatchPair 方法异常 当前字符开始不是leftPair leftPair:" + leftPair + " rightPair:" + rightPair + " debug:" + debug());
        }
        Stack<String> stack = new Stack<>();
        stack.push(leftPair);
        StringBuilder sb = new StringBuilder();
        while (stack.isEmpty() == false) {
            this.next();
            _c = current();
            switch (_c) {
                case "(":
                    stack.push("(");
                    break;
                case ")":
                    stack.pop();
                    break;
                default:
                    sb.append(_c);
                    break;
            }
        }
        // 读到了右边的符号 结束的符号
        return sb.toString();
    }
}
